package com.fd.dx.mapper;

import com.fd.dx.domain.ZP;
import com.fd.dx.sqlprovider.ZPProvider;
import com.fd.dx.web.domain.Page;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

@Mapper
public interface ZPMapper {
    /**
     * 获得家族一共有多少代。
     * @return 返回代的列表
     */
    @Select("SELECT DISTINCT gene FROM public.zp order by gene")
    public List<Integer> daList();
    /**
     * 获得每一代的人数
     * @return
     */
    @Select("SELECT count(*) FROM public.zp where gene=#{gene}")
    public Integer getDaiCountfromGene(Integer gene);
    /**
     * 获得每个人的子节点
     * @return
     */
    @Select("SELECT * FROM public.zp where pid=#{pid}")
    public List<ZP> getChildfromPid(Integer pid);
    /**
     * 获得兄弟节点
     * @return
     */
    @Select("SELECT * FROM public.zp where pid=#{pid} and id!=#{id}")
    public List<ZP> getXD(Map map);
    /**
     * 获得每个人的父亲节点
     * @return
     */
    @Select("SELECT * FROM public.zp where id=#{pid}")
    public ZP getParentfromPid(Integer pid);

    /**
     * 族谱数据一共有多少行
     * @return
     */
    @Select("SELECT count(*) FROM public.zp ")
    public Integer getTotalCount();
    /**
     * 分页查询
     * @return
     */
    @Select("SELECT * FROM public.zp order by id,gene,rank limit #{limit} offset #{offset}")
    public List<ZP> getDataFromPage(Page<ZP> page);
    /**
     * 模糊查询，根据名字进行查找
     * @return
     */
    @Select("SELECT * FROM public.zp where name like '%'|| #{name}||'%'")
    public List<ZP> finePersonFromName(String name);
    /**
     * 通过id获得节点
     * @return
     */
    @Select("SELECT * FROM public.zp where id =#{id}")
    public ZP finePersonFromID(Integer id);
    /**
     * 获得所有的id
     * @return
     */
    @Select("SELECT id FROM public.zp")
    public List<Integer> fineIDs();
    /**
     * 更新是否有孩子节点
     * @return
     */
    @Update("UPDATE public.zp SET haschild=#{haschild} WHERE id=#{id}")
    public Integer updateHasChild(Map map);
    @Select("SELECT count(*) FROM public.zp where name like '%'|| #{name}||'%'")
    public Integer getTotalCountFromName(String name);
    @Select("SELECT * FROM public.zp where name like '%'|| #{name}||'%'  order by id,gene,rank limit #{page.limit} offset #{page.offset}")
    public List<ZP> getDataFromPageAndName(Map map);

    @Select("SELECT count(*) FROM public.zp where rname like '%'|| #{rname}||'%'")
    public Integer getTotalCountFromRName(String rname);
    @Select("SELECT * FROM public.zp where rname like '%'|| #{rname}||'%'  order by id,gene,rank limit #{page.limit} offset #{page.offset}")
    public List<ZP> getDataFromPageAndRName(Map map);

    @Select("SELECT count(*) FROM public.zp where idname like '%'|| #{idname}||'%'")
    public Integer getTotalCountFromIDName(String idname);
    @Select("SELECT * FROM public.zp where idname like '%'|| #{idname}||'%'  order by id,gene,rank limit #{page.limit} offset #{page.offset}")
    public List<ZP> getDataFromPageAndIDName(Map map);
    @InsertProvider(type=ZPProvider.class, method="addZP")
    public Integer addZP(ZP zp);
    @UpdateProvider(type=ZPProvider.class, method="updateZP")
    public Integer updateZP(ZP zp);
    @DeleteProvider(type=ZPProvider.class, method="deleteZP")
    public Integer deleteZP(Integer id);
    @Select("SELECT max(id)+1 FROM public.zp")
    public Integer getId();
}
